<?php session_start()?>
<?php include 'db.php' ?>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" language="javascript" type="text/javascript">
</script>
<script src="js/jquery.colorbox.js" language="javascript" type="text/javascript">
</script>
<script src="js/hkpool.js" language="javascript" type="text/javascript">
</script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
	defaultIfEmpty("#name", "Your Name");
	defaultIfEmpty("#email", "Your Email");
	defaultIfEmpty("#password", "........");
	defaultIfEmpty("#confirm-password", "........");
	
	defaultIfEmpty("#signin-email", "Registered Email");
	defaultIfEmpty("#signin-password", "........");
});
</script>
<link href="styles/default.css" rel="stylesheet" type="text/css" />
</head>
<body style="background:#fff;padding:10px;">
<?php
$action=$_GET["action"];

$name=$_POST["name"];
$email=$_POST["email"];
$password=$_POST["password"];
$signin_email = $_POST["signin-email"];
$signin_password = $_POST["signin-password"];
$error = "";

$connection = mysqli_connect($db_host, $db_username, $db_password, $db_catalog);
if (mysqli_connect_errno())
{
	echo "Cannot open database connection. Please try again later. ". mysqli_connect_error();
	die();
}
if($action == "new"){
	$squery = "INSERT INTO people(name, email, password) VALUES ('".$name."', '".$email."', '".$password."')";
	$result = mysqli_query($connection, $squery);
	$result = mysqli_query($connection, "SELECT id FROM people WHERE email='".$email."' AND password ='".$password."'");
	if($result){
		$row = mysqli_fetch_array($result);
		if($row)
			$_SESSION["loggedin"] = $row['id'];
		else {
			$_SESSION["loggedin"] = 0;
			$error ="No user by that handle or invalid password";
		}
	}
}
else if ($action == "signin"){
	$result = mysqli_query($connection, "SELECT id FROM people WHERE email='".$signin_email."' AND password ='".$signin_password."'");
	if($result){
		$row = mysqli_fetch_array($result);
		if($row)
			$_SESSION["loggedin"] = $row['id'];
		else {
			$_SESSION["loggedin"] = 0;
			$error ="No user by that handle or invalid password";
		}
	}
}

if($_SESSION["loggedin"] != 0){
	echo "<script type='text/javascript'>$(document).ready(function () { window.parent.$.colorbox.close(); })</script>";
}
?>
	<h1>
	Sign Up or Login</h1>
	<span style="color:#f00;font-weight:bold;"><?php echo $error ?></span>
	<table>
			<thead>
				<td><b>New?<b></td>
				<td></td>
				<td><b>Existing<b></td>
			</thead>
			<tr>
				<td width="50%">
					<form action="login.php?action=new" method="post">
					<table class="pool-list">
						<tr>
							<td>
							
								<input type="text" id="name" name="name" value = "Your Name">
							</td>
						</tr>
						<tr>
							<td>
								<input type="text" id="email" name="email" value = "Your Email">
							</td>
						</tr>
						<tr>
							<td>
								<input type="password" id="password" name="password" value = "........">
							</td>
						</tr>
						<tr>
							<td>
								<input type="password" id="confirm-password" name="confirm-password" value = "........">
							</td>
						</tr>
						<tr>
							<td>
								<input type="submit" id="signup" value = "Sign Up">
							</td>
						</tr>
					</table>
					</form>
				</td>
				<td style="border-left:1px solid black;"></td>
					<td width="50%">
					<form action="login.php?action=signin" method="post">
					<table class="pool-list">
						<tr>
							<td>
								<input type="text" id="signin-email" name="signin-email" value="Registered Email">
							</td>
						</tr>
						<tr>
							<td>
								<input type="password" id="signin-password" name="signin-password" value = "........">
							</td>
						</tr>
						<tr>
							<td>
								<input type="submit" id="signin" value = "Sign In">
							</td>
						</tr>
						
					</table>
					</form>
				</td>
			</tr>
		</table>
	</body>
</html>
<?php mysqli_close($connection); ?>
